<?php
/*
Page:           rpc.php
Created:        Aug 2006
Last Mod:       Mar 18 2007
This page handles the 'AJAX' type response if the user
has Javascript enabled.
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See credits.txt for full credit details.
--------------------------------------------------------- */

header("Cache-Control: no-cache");
header("Pragma: nocache");

require(dirname( dirname( dirname( dirname(__FILE__ )))).'/config/config.inc.php');
require(dirname( dirname(__FILE__ )).'/productratingtime.php');
require('_config-rating.php');

$productrating_time = new productratingtime();

//getting the values
$vote_sent_time = preg_replace('/[^0-9]/', '', $_REQUEST['j_time']);
$vote_sent_time = (int)$vote_sent_time;
$id_sent_time = preg_replace('/[^0-9a-zA-Z]/', '', $_REQUEST['q_time']);
$id_sent_time = (int)$id_sent_time;
$ip_num_time = preg_replace('/[^0-9\.]/', '', $_REQUEST['t_time']);
$units_time = preg_replace('/[^0-9]/', '', $_REQUEST['c_time']);
$units_time = (int)$units_time;
$ip_time = $_SERVER['REMOTE_ADDR'];

// kill the script because normal users will never see this.
if ($vote_sent_time > $units_time) 
	die('Sorry, vote appears to be invalid.'); 

// connecting to the database to get some information
$query_time = mysql_query("SELECT `total_votes`, `total_value`, `used_ips` FROM `$rating_dbname_time`.`$rating_tableName_time` WHERE `id` = '$id_sent_time'") or die("Error1: ".mysql_error());
$numbers_time = mysql_fetch_assoc($query_time);

$checkIP_time = unserialize($numbers_time['used_ips']);
$count_time = $numbers_time['total_votes']; // how many votes total
$current_rating_time = $numbers_time['total_value']; // total number of rating added together and stored
$sum_time = $vote_sent_time + $current_rating_time; // add together the current vote value and the total vote value

$tense_time = ($count_time == 1) ? 'vote': 'votes'; // plural form votes/vote


// checking to see if the first vote has been tallied 
// or increment the current number of votes
($sum_time == 0 ? $added_time = 0 : $added_time = $count_time + 1);

// if it is an array i.e. already has entries the push in another value
((is_array($checkIP_time)) ? array_push($checkIP_time, $ip_num_time) : $checkIP_time = array($ip_num_time));
$insertip_time = serialize($checkIP_time);

// IP check when voting
$voted_time = mysql_num_rows(mysql_query("SELECT `used_ips` FROM `$rating_dbname_time`.`$rating_tableName_time` WHERE `used_ips` LIKE '%".$ip_time."%' AND `id` = '".$id_sent_time."'"));
if (!$voted_time) 
	// if the user hasn't yet voted, then vote normally...
	if (($vote_sent_time >= 1 && $vote_sent_time <= $units_time)) 
	{ 
		// keep votes within range
		$update_time = "UPDATE `$rating_dbname_time`.`$rating_tableName_time` SET `total_votes` = '".$added_time."', `total_value` = '".$sum_time."', `used_ips` = '".$insertip_time."' WHERE `id` = '$id_sent_time'";
		$result_time = mysql_query($update_time);		
	} 

// these are new queries to get the new values!
$newtotals_time = mysql_query("SELECT `total_votes`, `total_value`, `used_ips` FROM `$rating_dbname_time`.`$rating_tableName_time` WHERE `id` = '$id_sent_time'") or die("Error2: ".mysql_error());
$numbers_time = mysql_fetch_assoc($newtotals_time);
$count_time = $numbers_time['total_votes']; // how many votes total
$current_rating_time = $numbers_time['total_value']; // total number of rating added together and stored
$tense_time = ($count_time == 1) ? 'vote': 'votes'; // plural form votes/vote

// $new_back_time is what gets 'drawn' on your page after a successful 'AJAX/Javascript' vote
$new_back_time = array();

$new_back_time[] .= '<ul class="unit-rating-time" style="width: '.$units_time * $rating_unitwidth_time.'px;">';
$new_back_time[] .= '<li class="current-rating-time" style="width: '.@number_format($current_rating_time / $count_time, 2) * $rating_unitwidth_time.'px;">Current rating.</li>';
$new_back_time[] .= '<li class="r1-unit">1</li>';
$new_back_time[] .= '<li class="r2-unit">2</li>';
$new_back_time[] .= '<li class="r3-unit">3</li>';
$new_back_time[] .= '<li class="r4-unit">4</li>';
$new_back_time[] .= '<li class="r5-unit">5</li>';
$new_back_time[] .= '<li class="r6-unit">6</li>';
$new_back_time[] .= '<li class="r7-unit">7</li>';
$new_back_time[] .= '<li class="r8-unit">8</li>';
$new_back_time[] .= '<li class="r9-unit">9</li>';
$new_back_time[] .= '<li class="r10-unit">10</li>';
$new_back_time[] .= '</ul>';
$new_back_time[] .= '<p class="voted">'.$productrating_time->traduz('Rating').': <strong>'.@number_format($sum_time / $added_time, 1).'</strong>/'.$units_time.' ('.$count_time.' '.$tense_time.' '.$productrating_time->traduz('cast').')';
$new_back_time[] .= '<br /><span class="thanks">'.$productrating_time->traduz('Thanks for voting!').'</span></p>';

$allnewback_time = join("\n", $new_back_time);

// name of the div id to be updated | the html that needs to be changed
$output_time = "unit_long_time$id_sent_time|$allnewback_time";
echo $output_time;